package com.mzw.bizmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 用于创建测试程序用到的交换机和队列（只用在程序启动前执行一次）
 */
@Configuration
public class BiInitMain {


    // 声明交换机
    @Bean
    public DirectExchange biExchange() {
        return new DirectExchange(BiMqConstant.BI_EXCHANGE_NAME);
    }

    // 声明死信交换机
    @Bean
    public DirectExchange biDeadExchange() {
        return new DirectExchange(BiMqConstant.BI_DEAD_EXCHANGE_NAME);
    }

    // 声明死信队列
    @Bean
    public Queue biDeadQueue() {
        return new Queue(BiMqConstant.BI_DEAD_QUEUE_NAME);
    }

    // 声明队列
    @Bean
    public Queue biQueue() {
//        Map<String, Object> args = new HashMap<>();
//        //声明当前队列绑定的死信交换机
//        args.put("x-dead-letter-exchange", BiMqConstant.BI_DEAD_EXCHANGE_NAME);
//        //声明当前队列的死信路由 key
//        args.put("x-dead-letter-routing-key", BiMqConstant.BI_DEAD_ROUTING_KEY);
        //没有声明 TTL 属性
        return QueueBuilder.durable(BiMqConstant.BI_QUEUE_NAME)
                .deadLetterExchange(BiMqConstant.BI_DEAD_EXCHANGE_NAME)
                .deadLetterRoutingKey(BiMqConstant.BI_DEAD_ROUTING_KEY)
                .build();
//        return QueueBuilder.durable(BiMqConstant.BI_QUEUE_NAME).build();
    }


    // 声明确认绑定关系
    @Bean
    public Binding queueBinding(Queue biQueue, DirectExchange biExchange) {
        return BindingBuilder.bind(biQueue).to(biExchange).with(BiMqConstant.BI_ROUTING_KEY);
    }


    // 死信交换机绑定死信队列
    @Bean
    public Binding deadBinding(Queue biDeadQueue, DirectExchange biDeadExchange) {
        return BindingBuilder.bind(biDeadQueue).to(biDeadExchange).with(BiMqConstant.BI_DEAD_ROUTING_KEY);
    }
}
